package ui.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AddOwner;
import com.fy.fyzf.bean.LookOwnerBean;
import com.fy.fyzf.bean.OwnerListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.dialog.AddOwnerdialog;
import com.fy.fyzf.listener.PermissionListener;
import com.fy.fyzf.presenter.OwnerListPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.PhoneFormatCheckUtils;
import com.fy.fyzf.view.OwnerListCallBack;

import java.util.List;

import butterknife.BindView;
import ui.adapter.OwnerListAdapter;

public class OwnerListActivity extends BaseActivity<OwnerListPresenter> implements OwnerListCallBack {


    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private OwnerListAdapter ownerListAdapter;
    private Integer housingId;
    private Integer housingStatus;

    @Override
    protected OwnerListPresenter createPresenter() {
        return new OwnerListPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_owner_list;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("看业主");
        ownerListAdapter = new OwnerListAdapter();
        recycleView.setLayoutManager(new LinearLayoutManager(this));
//        recycleView.addItemDecoration(new SpacesItemDecoration(20));
        recycleView.setAdapter(ownerListAdapter);
        View head = LayoutInflater.from(this).inflate(R.layout.head_add_owner, null, false);

        ownerListAdapter.addHeaderView(head);

        ownerListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<OwnerListBean.ListOwnerVosBean> data = adapter.getData();
                requestRuntimePermission(new String[]{Manifest.permission.CALL_PHONE}, new PermissionListener() {
                    @Override
                    public void onGranted() {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.get(position).getOwnerMobile()));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }

                    @Override
                    public void onDenied(List<String> deniedPermissions) {
                        AppUtils.showToast("拨号权限被拒绝");
                    }
                });
            }
        });
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddOwnerdialog addOwnerdialog = new AddOwnerdialog(OwnerListActivity.this);
                addOwnerdialog.toggleDialog();
                addOwnerdialog.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(addOwnerdialog.etName.getText().toString())) {
                            AppUtils.showToast("请输入业主姓名");
                            return;
                        }
                        if (TextUtils.isEmpty(addOwnerdialog.etTel.getText().toString())) {
                            AppUtils.showToast("请输入业主电话");
                            return;
                        }
                        AddOwner addOwner = new AddOwner();

                        addOwner.setOwnerName(addOwnerdialog.etName.getText().toString());
                        addOwner.setOwnerMobile(addOwnerdialog.etTel.getText().toString());
                        addOwner.setOwnerRemark(addOwnerdialog.etRemark.getText().toString());
                        addOwner.setHousingId(housingId);
                        mPresenter.addOwner(addOwner);
                        addOwnerdialog.toggleDialog();
                    }
                });
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        housingId = getIntent().getIntExtra("housingId", 0);
        housingStatus = getIntent().getIntExtra("housingStatus", 0);
        LookOwnerBean lookOwnerBean = new LookOwnerBean();
        lookOwnerBean.setHousingId(housingId);
        lookOwnerBean.setHousingStart(housingStatus);
        mPresenter.getOwnerList(lookOwnerBean);
    }

    @Override
    public void getOwnerListSuccess(OwnerListBean ownerListBean) {
        ownerListAdapter.setNewData(ownerListBean.getListOwnerVos());
    }

    @Override
    public void addOwnerSuccess(BaseData baseData) {
        initData();
    }

    @Override
    public void onError() {

    }
}